import web
from config.setting import db

def add_artist(member_id, member_name):
    ret = db.insert('artist', artist_id = member_id, artist_name = member_name)
    return ret

def get_artist(member_id):
    sql_var = dict(member_id = member_id)
    ret = db.select('artist', where = 'artist_id = $member_id', vars = sql_var)
    return ret

def get_artist_list(limit = (0, 10), order = 'ASC'):
    #$limit = ($page - 1) * 15 . ', 15';
    ret = db.query('SELECT artist.id, artist.artist_id, artist.artist_name, artist.avatar, COALESCE(artist_illust.total, 0) AS total \
    FROM artist LEFT JOIN artist_illust ON \
    artist.artist_id = artist_illust.artist_id \
    ORDER BY artist.id %s \
    LIMIT %s, %s' % (order, limit[0], limit[1]))
    return ret

def del_artist(member_id):
    sql_var = dict(member_id = member_id)
    ret = db.delete('artist', where = 'artist_id = $member_id', vars = sql_var)
    return ret

def get_artist_info(member_id):
    sql_var = dict(member_id = member_id)
    ret = db.query('SELECT \
    artist.id, artist.artist_id, artist.artist_name, artist.avatar, artist_illust.total \
    FROM artist LEFT JOIN artist_illust \
    ON artist.artist_id = artist_illust.artist_id \
    WHERE artist.artist_id = $member_id', sql_var)
    return ret